package com.adcc.loadsheet.billing.util;

import com.adcc.loadsheet.billing.entity.export.ExcelItem;
import com.google.common.base.Strings;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 导出EXCEL
 * Created by zhaoml on 2020/5/9.
 */
public class ExcelUtil {
    /**
     * 包含工作表（计费统计）
     * @param excelItems
     * @return
     */
    public static HSSFWorkbook getHSSFWorkbook(List<ExcelItem> excelItems) {
        HSSFWorkbook wb = new HSSFWorkbook();
        for(ExcelItem excelItem : excelItems){
            wb = getStatisticsHSSFWorkbook(
                    excelItem.getSheetName(),excelItem.getTitle(),excelItem.getRemark(), excelItem.getTitles(), excelItem.getValues(), wb);
        }
        return wb;
    }

    //不包含工作表
    public static HSSFWorkbook getHSSFWorkbook(String sheetName, String[] title, String[][] values) {
        return getHSSFWorkbook(sheetName, title, values, null);
    }

    /**
     * 导出Excel
     *
     * @param sheetName sheet名称
     * @param titles     标题
     * @param values    内容
     * @param wb        HSSFWorkbook对象
     * @return
     */
    public static HSSFWorkbook getHSSFWorkbook(String sheetName,String[] titles, String[][] values, HSSFWorkbook wb) {
        // 第一步，创建一个HSSFWorkbook，对应一个Excel文件
        if (wb == null) {
            wb = new HSSFWorkbook();
        }

        // 第二步，在workbook中添加一个sheet,对应Excel文件中的sheet
        HSSFSheet sheet = wb.createSheet(sheetName);
        sheet.setDefaultColumnWidth(20); //调整列宽度

        // 第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制
        HSSFRow row = sheet.createRow(0);

        // 第四步，创建单元格，并设置值表头 设置表头居中
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER); // 创建一个居中格式
        style.setVerticalAlignment(VerticalAlignment.CENTER); // 创建一个垂直居中格式

        //声明列对象
        HSSFCell cell = null;

        //创建标题
        for (int i = 0; i < titles.length; i++) {
            cell = row.createCell(i);
            cell.getCellStyle().setVerticalAlignment(VerticalAlignment.CENTER);
            cell.getCellStyle().setAlignment(HorizontalAlignment.CENTER);
            cell.setCellValue(titles[i]);
            cell.setCellStyle(style);
        }

        //创建内容
        for (int i = 0; i < values.length; i++) {
            row = sheet.createRow(i + 1);
            for (int j = 0; j < values[i].length; j++) {
                //将内容按顺序赋给对应的列对象
                row.createCell(j).setCellValue(values[i][j]);
            }
        }
        return wb;
    }

    /**
     * 导出Excel
     *
     * @param sheetName sheet名称
     * @param titles     标题
     * @param values    内容
     * @param wb        HSSFWorkbook对象
     * @return
     */
    public static HSSFWorkbook getStatisticsHSSFWorkbook(
            String sheetName,String title,String remark, String[] titles, String[][] values, HSSFWorkbook wb) {
        // 第一步，创建一个HSSFWorkbook，对应一个Excel文件
        if (wb == null) {
            wb = new HSSFWorkbook();
        }

        // 第二步，在workbook中添加一个sheet,对应Excel文件中的sheet
        HSSFSheet sheet = wb.createSheet(sheetName);
        sheet.setDefaultColumnWidth(20); //调整列宽度

        // 第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制
        HSSFRow row = sheet.createRow(0);

        // 第四步，创建单元格，并设置值表头 设置表头居中
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER); // 创建一个居中格式
        style.setVerticalAlignment(VerticalAlignment.CENTER); // 创建一个垂直居中格式

        //声明列对象
        HSSFCell cell = null;

        //中间标题
        row = sheet.createRow(0);
        row.createCell(0).setCellValue(title);
        //年份
        row = sheet.createRow(1);
        row.createCell(0).setCellValue(remark);

        //创建标题
        row = sheet.createRow(2);
        for (int i = 0; i < titles.length; i++) {
            cell = row.createCell(i);
            cell.getCellStyle().setVerticalAlignment(VerticalAlignment.CENTER);
            cell.getCellStyle().setAlignment(HorizontalAlignment.CENTER);
            cell.setCellValue(titles[i]);
            cell.setCellStyle(style);
        }

        //创建内容
        for (int i = 0; i < values.length; i++) {
            row = sheet.createRow(i + 3);
            for (int j = 0; j < values[i].length; j++) {
                //将内容按顺序赋给对应的列对象
                row.createCell(j).setCellValue(values[i][j]);
            }
        }
        return wb;
    }

    //响应到客户端
    public static void returnClient(HttpServletResponse response, String fileName, HSSFWorkbook wb) {
        try {
            setResponseHeader(response, fileName);
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //发送响应流方法
    public static void setResponseHeader(HttpServletResponse response, String fileName) {
        try {
            try {
                fileName = new String(fileName.getBytes(), "ISO8859-1");
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            response.setContentType("application/octet-stream;charset=ISO8859-1");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}